<%@ Control Language="C#" ClassName="StateFlows" Inherits="Dimok.Web.UI.TableControl" %>
<%@ Register Namespace="Dimok.Web.UI"  Assembly="Dimok.Web.UI" TagPrefix="OR"  %>
<%@ Register Namespace="Data.DataSources" Assembly="App_Code" TagPrefix="DS"  %>
<%@ Register Src="StateFlow_Groups.ascx" TagName="StateFlow_Groups" TagPrefix="WC" %>
<%@ Register Src="StateFlow_ActionsByPath.ascx" TagName="StateFlow_ActionsByPath" TagPrefix="WC" %>

<script runat="server">
	protected void OnRowCreated(object sender, GridViewRowEventArgs e) {
		GridViewEx grv = (GridViewEx)sender;
		if((e.Row.RowState & DataControlRowState.Edit) != DataControlRowState.Edit) return;
		
		StateFlow_Groups sfg = (StateFlow_Groups)e.Row.Cells[e.Row.Cells.Count - 2].FindControl("StateFlow_Groups");
		sfg.StateFromID = int.Parse("0" + grv.EditKeys["StateFromID"]);
		sfg.StateToID = int.Parse("0" + grv.EditKeys["StateToID"]);

		StateFlow_ActionsByPath sfa = (StateFlow_ActionsByPath)e.Row.Cells[e.Row.Cells.Count - 1].FindControl("StateFlow_ActionsByPath");
		sfa.DataBinding += new EventHandler(afg_DataBinding);
	}

	void afg_DataBinding(object sender, EventArgs e) {
		StateFlow_ActionsByPath sfa = (StateFlow_ActionsByPath)sender;
		sfa.StateID = int.Parse("0" + grv.EditKeys["StateFromID"]);
	}
</script>
<asp:UpdatePanel ID="pnl" UpdateMode="Always" runat="server">
	<ContentTemplate>
		<OR:GridViewEx ID="grv" runat="server" AutoGenerateColumns="False" DataSourceID="ds"
			ShowFooter="True" AllowSorting="True" BackColor="Window" CellPadding="0" EnableTheming="True"
			Font-Names="verdana" EnableViewState="False" DataKeyNames="ID,StateFromID,StateToID"
			ViewNameUpdate="t_StateFlow" ViewNameInsert="t_StateFlow" OnRowCreated="OnRowCreated"
			DebugMode="false"
			>
			<Columns>

				<!-- #include virtual = "~/Controls/_FieldTemplates/ID_.txt" -->

				<asp:TemplateField HeaderText="TableName" SortExpression="TableNameFrom">
					<ItemTemplate><asp:Label Text='<%#Eval("TableNameFrom") %>' runat="server" /></ItemTemplate>
					<EditItemTemplate><asp:Label Text='<%#Eval("TableNameFrom") %>' runat="server" /></EditItemTemplate>
				</asp:TemplateField>

				<asp:TemplateField HeaderText="From" SortExpression="NameFrom">
					<ItemTemplate>
						<asp:Label Text='<%#Eval("NameFrom") %>' runat="server" />
						<asp:TextBox style="display:none" ID="txtStateFromID"  runat="server" Text='<%# Bind("StateFromID") %>' />
					</ItemTemplate>
					<EditItemTemplate>
						<asp:DropDownList ID="selStateFromID" DataSourceID="dsStates" DataTextField="NameLong" DataValueField="ID" runat="server"
								 SelectedValue='<%# Bind("StateFromID") %>'></asp:DropDownList>
					</EditItemTemplate>
					<FooterTemplate>
						<asp:DropDownList ID="StateFromID" DataSourceID="dsStates" DataTextField="NameLong" DataValueField="ID" runat="server"></asp:DropDownList>
					</FooterTemplate>
				</asp:TemplateField>

				<asp:TemplateField HeaderText="To" SortExpression="NameTo">
					<ItemTemplate>
						<asp:Label Text='<%#Eval("NameTo") %>' runat="server" />
						<asp:TextBox style="display:none" ID="txtStateToID"  runat="server" Text='<%# Bind("StateToID") %>' />
					</ItemTemplate>
					<EditItemTemplate>
						<asp:DropDownList ID="selStateToID" DataSourceID="dsStates" DataTextField="NameLong" DataValueField="ID" runat="server"
								 SelectedValue='<%# Bind("StateToID") %>'></asp:DropDownList>
					</EditItemTemplate>
					<FooterTemplate>
						<asp:DropDownList ID="StateToID" DataSourceID="dsStates" DataTextField="NameLong" DataValueField="ID" runat="server"></asp:DropDownList>
					</FooterTemplate>
				</asp:TemplateField>

				<asp:TemplateField HeaderText="Direction" SortExpression="Dir">
					<ItemTemplate><asp:Label Text='<%#Eval("Dir") %>' runat="server" /></ItemTemplate>
					<EditItemTemplate><asp:Label Text='<%#Eval("Dir") %>' runat="server" /></EditItemTemplate>
				</asp:TemplateField>

				<asp:TemplateField HeaderText="Groups" SortExpression="Groups">
					<ItemTemplate>Groups</ItemTemplate>
					<EditItemTemplate>
						<WC:StateFlow_Groups ID="StateFlow_Groups" ConnectionString="<%$ ConnectionStrings:Boris_2006 %>" runat="server" />
					</EditItemTemplate>
				</asp:TemplateField>
				
				<asp:TemplateField HeaderText="Actions" SortExpression="Actions">
					<ItemTemplate>Actions</ItemTemplate>
					<EditItemTemplate>
						<WC:StateFlow_ActionsByPath ID="StateFlow_ActionsByPath" runat="server" />
					</EditItemTemplate>
				</asp:TemplateField>
			</Columns>
		</OR:GridViewEx>
	</ContentTemplate>
</asp:UpdatePanel>

<DS:StateFlowDataSource ID="ds" runat="server" />
<DS:StatesDataSource ID="dsStates" runat="server" />
